/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.ui.template;

import com.bytedance.scene.navigation.NavigationSceneGetter;
import com.bytedance.scene.ui.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

/**
 * Created by JiangQi on 8/29/18.
 */
abstract class SwipeBackAppCompatScene extends SwipeBackGroupScene {
    private StackLayout slBack;
    protected Text textTitle;
    protected DependentLayout toolbar;
    protected StackLayout slMore;

    @Override
    protected Component onCreateSwipeContentView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        Component view = inflater.parse(ResourceTable.Layout_scene_appcompat_layout, container, false);
        this.slBack = (StackLayout) view.findComponentById(ResourceTable.Id_sl_appcompat_layout_back);
        this.textTitle = (Text) view.findComponentById(ResourceTable.Id_text_appcompat_layout_title);
        this.toolbar = (DependentLayout) view.findComponentById(ResourceTable.Id_toolbar);
        this.slMore = (StackLayout) view.findComponentById(ResourceTable.Id_sl_appcompat_layout_more);
        StackLayout mContentLayout = (StackLayout) view.findComponentById(ResourceTable.Id_sl_appcompat_layout_content);

        Component contentView = onCreateContentView(inflater, mContentLayout, savedInstanceState);
        if (contentView != null) {
            mContentLayout.addComponent(contentView);
        }
        return view;
    }

    protected abstract Component onCreateContentView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState);

    @Override
    public void onAbilityCreated(Intent savedInstanceState) {
        super.onAbilityCreated(savedInstanceState);
        this.slBack.setClickedListener(component -> NavigationSceneGetter.requireNavigationScene(SwipeBackAppCompatScene.this).pop());
    }

    /**
     * 设置title
     *
     * @param title
     */
    public void setTitle(CharSequence title) {
        this.textTitle.setText(title.toString());
    }

    /**
     * 是否显示bar
     *
     * @param visible
     */
    public void setTitleVisiable(int visible){
        this.toolbar.setVisibility(visible);
    }
}
